package com.cby.widget.sample.fragments

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.OvershootInterpolator
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.cby.widget.lib.blur.BlurringView
import com.cby.widget.lib.kotlin.*
import com.cby.widget.sample.R
import java.util.*

/**
 * @description: BlurFragment
 *
 * @author ChenBaoyang
 * @date 2021/10/3 15:06
 */
class BlurFragment : Fragment() {

    private lateinit var mBlurringView: BlurringView

    companion object{
        private val IMAGE_IDS = intArrayOf(
            R.drawable.p0, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9
        )
    }

    private val mImageViews = arrayOfNulls<ImageView>(9)
    private var mStartIndex = 0

    private val mRandom = Random()

    private var mShifted = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_blur, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBlurringView = view.findViewById<View>(R.id.blurring_view) as BlurringView
        val blurredView: View = view.findViewById<View>(R.id.blurred_view)

        mBlurringView.setBlurredView(blurredView)

        mImageViews[0] = view.findViewById<View>(R.id.image0) as ImageView?
        mImageViews[1] = view.findViewById<View>(R.id.image1) as ImageView?
        mImageViews[2] = view.findViewById<View>(R.id.image2) as ImageView?
        mImageViews[3] = view.findViewById<View>(R.id.image3) as ImageView?
        mImageViews[4] = view.findViewById<View>(R.id.image4) as ImageView?
        mImageViews[5] = view.findViewById<View>(R.id.image5) as ImageView?
        mImageViews[6] = view.findViewById<View>(R.id.image6) as ImageView?
        mImageViews[7] = view.findViewById<View>(R.id.image7) as ImageView?
        mImageViews[8] = view.findViewById<View>(R.id.image8) as ImageView?

        val shuffleBtn = view.findViewById<View>(R.id.shuffle_button)
        val shiftBtn = view.findViewById<View>(R.id.shift_button)
        shuffleBtn.setOnClickListener {
            shuffle()
        }
        shiftBtn.setOnClickListener {
            shift()
        }
    }

    fun shuffle() {
        var newStartIndex: Int
        do {
            newStartIndex = IMAGE_IDS[mRandom.nextInt(IMAGE_IDS.size)]
        } while (newStartIndex == mStartIndex)
        mStartIndex = newStartIndex

        for (i in mImageViews.indices) {
            val drawableId: Int = IMAGE_IDS[(mStartIndex + i) % IMAGE_IDS.size]
            mImageViews[i]!!.setImageDrawable(resources.getDrawable(drawableId))
        }
        mBlurringView.invalidate()
    }

    private val listener = AnimatorUpdateListener { mBlurringView.invalidate() }

    fun shift() {
        if (!mShifted) {
            for (imageView in mImageViews) {
                val tx = ObjectAnimator.ofFloat(
                    imageView,
                    View.TRANSLATION_X,
                    (mRandom.nextFloat() - 0.5f) * 500
                )
                tx.addUpdateListener(listener)
                val ty = ObjectAnimator.ofFloat(
                    imageView,
                    View.TRANSLATION_Y,
                    (mRandom.nextFloat() - 0.5f) * 500
                )
                ty.addUpdateListener(listener)
                val set = AnimatorSet()
                set.playTogether(tx, ty)
                set.duration = 3000
                set.interpolator = OvershootInterpolator()
                set.addListener(imageView?.let {
                    AnimationEndListener(
                        it
                    )
                }
                )
                set.start()
            }
            mShifted = true
        } else {
            for (imageView in mImageViews) {
                val tx = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_X, 0f)
                tx.addUpdateListener(listener)
                val ty = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_Y, 0f)
                ty.addUpdateListener(listener)
                val set = AnimatorSet()
                set.playTogether(tx, ty)
                set.duration = 3000
                set.interpolator = OvershootInterpolator()
                set.addListener(imageView?.let {
                    AnimationEndListener(
                        it
                    )
                }
                )
                set.start()
            }
            mShifted = false
        }
    }


    private class AnimationEndListener(var mView: View) : Animator.AnimatorListener {
        override fun onAnimationStart(animation: Animator) {
            mView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }

        override fun onAnimationEnd(animation: Animator) {
            mView.setLayerType(View.LAYER_TYPE_NONE, null)
        }

        override fun onAnimationCancel(animation: Animator) {
            mView.setLayerType(View.LAYER_TYPE_NONE, null)
        }

        override fun onAnimationRepeat(animation: Animator) {}
    }
}